package com;


import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ReadXML {

    private static DocumentBuilderFactory documentBuilderFactory= null;
    private static DocumentBuilder documentBuilder = null;
    private static Document document = null;
    static {
        try {
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    public static Map<String,String> getThemeValue(String xmlName){
        Map<String,String> map = new HashMap<>();
        File file = new File(xmlName);
        try {
            document = documentBuilder.parse(file);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Element root = document.getDocumentElement();
        for (int i = 1; i < root.getChildNodes().getLength(); i+=2) {
            String att = root.getChildNodes().item(i).getAttributes().item(0).getNodeValue();
            String value = root.getChildNodes().item(i).getTextContent();
            map.put(att,value);
        }

        return map;
    }

}
